// 导入 createWebHistory
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
const Parent = () => import('@/view/layout/parent.vue')

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'layout',
    component: () => import('@/view/layout/index.vue'),
    redirect: '/news',
    children: [
      {
        path: 'news',
        component: () => import('@/view/news/index.vue'),
        meta: { title: 'news' },
      },
      {
        path: 'news/detail',
        component: () => import('@/view/news/detail.vue'),
        meta: { title: 'newsDetail' },
      },
      {
        path: 'events',
        component: () => import('@/view/events/index.vue'),
        meta: { title: 'events' },
      },
      {
        path: 'events/detail',
        component: () => import('@/view/events/detail.vue'),
        meta: { title: 'eventsDetail' },
      },
      {
        path: 'events/review_detail',
        component: () => import('@/view/events/reviewDetail.vue'),
        meta: { title: 'eventsReviewDetail' },
      },
      {
        path: 'cases',
        component: () => import('@/view/cases/index.vue'),
        meta: { title: 'cases' },
      },
      {
        path: 'cases/detail',
        component: () => import('@/view/cases/detail.vue'),
        meta: { title: 'casesDetail' },
      },
      {
        path: 'user/center',
        component: () => import('@/view/user/center.vue'),
        meta: { title: 'userCenter' },
      },
      {
        path: 'about',
        component: () => import('@/view/about/index.vue'),
        meta: { title: 'about' },
      },
      {
        path: 'contact',
        component: () => import('@/view/contact/index.vue'),
        meta: { title: 'contact' },
      }
    ],
  },
  {
    path: '/login',
    component: () => import('@/view/layout/login.vue'),
    meta: { title: 'login', auth: false },
  },
  //页面中的二级页面
  {
    path: '/user',
    name: 'userPage',
    component: Parent,
    children: [
      {
        path: 'detail',
        name: 'detail',
        component: () => import('@/view/user/detail.vue'),
        meta: {
          title: '用户二级页面',
          pass: true,
        },
      },
    ],
  },

  {
    path: '/404',
    name: 'NotFound',
    component: () => import('@/view/layout/404.vue'),
    meta: {
      title: '404',
      auth: false,
    },
  },
  {
    //不存在的路由
    path: '/:pathMatch(.*)',
    redirect: '/404',
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to) {
    if (to.hash) {
      return {
        el: to.hash,
        behavior: 'smooth',
      }
    }
  },
})

export default router
